package com.szty.libarytest.longConnect.manager

import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import com.blankj.utilcode.util.GsonUtils
import com.szty.libarytest.other.loge
import com.szty.libarytest.longConnect.DotBean

/**
 *    author : SunJH
 *    date   : 2021/4/30 0030
 *    desc   : 发送笔迹点到服务器的服务
 */
class SendPointService:Service() {

    class SendBind:Binder(){

        fun sendPointToService(dot: DotBean){
            val json = GsonUtils.getGson().toJson(dot)
            SessionManager.getInstance().writeToServer(json)
        }
        fun savePointToLocation(dot: DotBean){
            //TODO 需要保存点到本地数据库
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        "onStartCommand".loge()
        return super.onStartCommand(intent, flags, startId)

    }

    override fun onBind(intent: Intent?): IBinder {
        "onBind".loge()
        return SendBind()
    }

    override fun onUnbind(intent: Intent?): Boolean {
        "onUnbind".loge()
        return super.onUnbind(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        ClientConnectManager.getInstance().disConnect()
        SessionManager.getInstance().closeSession()
        SessionManager.getInstance().removeSession()
        "onDestroy".loge()
    }

    override fun onCreate() {
        super.onCreate()
        ClientConnectManager.getInstance().init(this)
//        if( SessionManager.getInstance().getmSession() == null){
//            ClientConnectManager.getInstance().connect(this)
//        }
        "onCreate".loge()
    }

    override fun onStart(intent: Intent?, startId: Int) {
        super.onStart(intent, startId)
        "onStart".loge()
    }

}